package com.i2863.receipt.controller;

import com.i2863.receipt.entity.Activity;
import com.i2863.receipt.entity.Prize;
import com.i2863.receipt.entity.Site;
import com.i2863.receipt.service.ActivityService;
import com.i2863.receipt.service.PrizeService;
import com.i2863.receipt.service.SiteService;
import com.i2863.receipt.util.ResultJson;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/6/8.
 */
@Controller
@RequestMapping("/prize")
public class PrizeController {

    @Autowired
    private PrizeService prizeService;

    @Autowired
    private SiteService siteService;

    @Autowired
    private ActivityService activityService;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private HttpServletResponse response;

    private final static Logger logger = LoggerFactory.getLogger(ActivityController.class);

    /**
     * 奖品列表
     * @param modelMap
     * @return
     */
    @RequestMapping("/getlist")
    public String getList(ModelMap modelMap){
        HttpSession session = request.getSession();
        Integer siteId = (Integer) session.getAttribute("siteId");
        List<Prize> list = prizeService.findBySiteId(siteId);
        modelMap.put("list",list);
        modelMap.put("total",list.size());
        return "prize/list";
    }

    /**
     * 跳转奖品添加页面
     * @return
     */
    @RequestMapping("/toAdd")
    public String toAdd(){
        return "prize/add";
    }

    /**
     * 保存方法
     * @param prize
     * @return
     */
    @RequestMapping("/save")
    @ResponseBody
    public String save(Prize prize){
        HttpSession session = request.getSession();
        Integer siteId = (Integer) session.getAttribute("siteId");
        String prizeName = prize.getPrizeName();
        if(StringUtils.isNotBlank(prizeName)){
            Integer prizeCount = prizeService.countLevelPrizeBySiteId(prize.getLevel(),siteId);
            prize.setPrize(prizeCount+1);//如果有奖项名称  就算新加一个奖项
        }
        Site site = siteService.findOne(siteId);
        prize.setSite(site);
        prizeService.add(prize);
        return ResultJson.successMeg("保存成功");
    }

    /**
     * 跳转编辑页面
     * @return
     */
    @RequestMapping("/toedit")
    public String toEdit(@RequestParam(value = "prizeId",required = true)Integer prizeId,ModelMap modelMap){
        Prize entity = prizeService.findOne(prizeId);
        modelMap.put("prize",entity);
        return "prize/edit";
    }

    /**
     * 修改实体
     * @param prize
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public String update(Prize prize){
        //查询当前站点下的活动  如果有正在抽奖中的活动  则不能删除奖品表
        HttpSession session = request.getSession();
        Integer siteId = (Integer) session.getAttribute("siteId");
        Prize entity = prizeService.findOne(prize.getId());
        entity.setName(prize.getName());
        entity.setLevel(prize.getLevel());
        entity.setMoney(prize.getMoney());
        entity.setNumber(prize.getNumber());
        entity.setPrizeName(prize.getPrizeName());
        entity.setPrizeRange(prize.getPrizeRange());
        prizeService.update(prize);
        return ResultJson.successMeg("修改成功");
    }

    @RequestMapping("/del")
    @ResponseBody
    public String del(@RequestParam(value = "ids")String ids){
        HttpSession session = request.getSession();
        Integer siteId = (Integer) session.getAttribute("siteId");
        String[] idArray = ids.split(",");
        List<Integer> idlist = new ArrayList<Integer>();
        for (String id : idArray) {
            idlist.add(Integer.parseInt(id));
        }
        List<Activity> activities = activityService.findListBySiteId(siteId);//查询当前站点下的活动  如果有正在抽奖中的活动  则不能删除奖品表
        for (Activity activity : activities) {
            Integer status = activity.getActivityStatus();
            if(status==Activity.STARTED||status==Activity.SECOND_ROUND){
                return ResultJson.errorMes("有活动正在进行中,不能删除奖品");
            }
        }
        for (Integer id:idlist){
            prizeService.del(id);
        }
        return ResultJson.successMeg("删除成功");
    }


}
